package com.ssy.controller;


import com.ssy.entity.Emp;
import com.ssy.entity.EmpQueryParam;
import com.ssy.entity.PageBean;
import com.ssy.entity.Result;
import com.ssy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * @param empQueryParam
     * @return
     */
    @GetMapping
    public Result Page(EmpQueryParam empQueryParam) {
        log.info("分页查询，参数：{}", empQueryParam);
        PageBean pageBean = empService.page(empQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 新增员工
     * @param emp
     * @return
     * @throws Exception
     */
    @PostMapping
    public Result save(@RequestBody Emp emp) throws Exception {
        log.info("新增员工: {}" , emp);
        empService.save(emp);
        return Result.success();
    }

    /**
     * 删除员工
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工: {}" , ids);
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询员工信息(查询回显)
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("查询员工信息: {}" , id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息 {}", emp);
        empService.update(emp);
        return Result.success();
    }

    /**
     * 查询所有员工
     */
    @GetMapping("/list")
    public Result list() {
        List<Emp> emps = empService.list();
        return Result.success(emps);
    }
}
